﻿/*----------------------------------------------------------------
// Copyright (C) 2009 
// 版权所有。 
//
// 文件名：
// 文件功能描述：系统用户信息管理
//
//
// 创建标识：
// 程序作者：翟允锋
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Threading;
using EQT.Model;
using EQT.Dal;
using Common.Logging;

public partial class TUser : System.Web.UI.Page
{
    private static readonly ILog log = LogManager.GetCurrentClassLogger();
    private DaTuser dalTuser = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTuser") as DaTuser;
    private DaEmployee daEmp = DevFuture.Common.WorkContext.AppCtx.GetObject("DaEmployee") as DaEmployee;
    private DaDept dalDept = DevFuture.Common.WorkContext.AppCtx.GetObject("DaDept") as DaDept;
    private DaTrole datrole = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTrole") as DaTrole;
    private DaTfiles dalFile = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTfiles") as DaTfiles;
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            this.Query();

            DataSet dsDept = dalDept.Query(string.Empty);

            Utility.BindDataSet2Combox(dsDept, CreateDept_ID, "Dept_Name", "Dept_ID");
            Utility.BindDataSet2Combox(dsDept, EditDept_ID, "Dept_Name", "Dept_ID");

            CreateDept_ID.Items.Insert(0, new ListItem("", ""));
            EditDept_ID.Items.Insert(0, new ListItem("", ""));

            this.CreateDept_ID_SelectedIndexChanged(null, null);
            this.EditDept_ID_SelectedIndexChanged(null, null);
            this.SetPanelVisiable(SummaryPanel);
            this.BindDDLRoles();
        }
    }

    private void BindDDLRoles()
    {
        DataSet ds = datrole.Query(null);
        Utility.BindDataSet2Combox(ds, CreateUserGroup, "ROLE_NAME", "ROLE_ID");
        Utility.BindDataSet2Combox(ds, EditUserGroup, "ROLE_NAME", "ROLE_ID");
    }

    protected void Query()
    {
        GridView.DataSource = dalTuser.Query();
        GridView.DataBind();
    }

    protected void EditSave_Click(object sender, EventArgs e)
    {
        if (EditPWD.Text.Trim() != EditPWDConfirm.Text.Trim())
        {
            OperationResult.Text = "两次密码输入不一致！请重新输入！"; divResult.Visible = true;
            return;
        }
        //执行保存操作。
        MoTuser mod = new MoTuser();
        mod.Uid = EditUID.Text.Trim();

        dalTuser.GetEntityEx(ref mod);

        mod.EmpId = EditEmp_ID.SelectedValue;
        mod.DisplayName = EditDisplay_Name.Text.Trim();
        mod.Pwd = EditPWD.Text.Trim();
        mod.Status = int.Parse(EditStatus.SelectedValue);
        mod.UpdateTime = DateTime.Now;

        EditPWD.Attributes.Add("Value", mod.Pwd);
        EditPWDConfirm.Attributes.Add("Value", mod.Pwd);


        try
        {

            int rtn = dalTuser.UpdateUser(mod, EditUserGroup.SelectedValue, HFEditRoleID.Value);

            if (rtn == 1)
            {
                OperationResult.Text = "用户信息修改成功！"; divResult.Visible = true;
                this.Query();
            }
            else
            {
                OperationResult.Text = "用户信息修改失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("用户信息修改失败", ex);
            OperationResult.Text = "用户信息修改失败！"; divResult.Visible = true;
        }
    }

    protected void EditBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        if (CreatePWD.Text.Trim() != CreatePWDConfirm.Text.Trim())
        {
            OperationResult.Text = "两次密码输入不一致！请重新输入！"; divResult.Visible = true;
            return;
        }
        if (string.IsNullOrEmpty(CreateEmp_ID.SelectedValue))
        {
            OperationResult.Text = "请选择员工！"; divResult.Visible = true;
            return;
        }
        try
        {
            MoTuser mod = new MoTuser();
            //TODO:创建的用户必须是员工
            mod.EmpId = CreateEmp_ID.SelectedValue;
            mod.Uid = CreateEmp_ID.SelectedValue;

            mod.DisplayName = CreateDisplay_Name.Text.Trim();
            mod.Pwd = CreatePWD.Text.Trim();
            mod.Status = 2;//2表示该用户是正常用户，-1表示被删除。0表示被禁用。默认为2.
            mod.CreateTime = DateTime.Now;
            mod.UpdateTime = DateTime.Now;

            int rtn = dalTuser.CreateUser(mod, CreateUserGroup.SelectedValue);

            if (rtn == 1)
            {
                this.CreateDept_ID_SelectedIndexChanged(null, null);
                OperationResult.Text = "用户信息创建成功！"; divResult.Visible = true;
                this.Query();
            }
            else
            {
                OperationResult.Text = "用户信息创建失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("用户信息创建失败", ex);
            OperationResult.Text = "用户信息创建失败！"; divResult.Visible = true;
        }
    }

    protected void CreateBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateGo_Click(object sender, EventArgs e)
    {
        this.CreateCancel();
        this.SetPanelVisiable(CreatePanel);
    }

    protected void DetailBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void EditGo_Click(object sender, EventArgs e)
    {
        string strKey = DetailUID.Text;
        this.DisplayEditInfo(strKey);
        this.SetPanelVisiable(EditPanel);
    }

    protected void DeleteGo_Click(object sender, EventArgs e)
    {
        try
        {
            string strKey = DetailUID.Text.Trim();

            int rtn = this.DeleteInfo(strKey, HDDetailRoleID.Value);
            if (rtn == 1)
            {
                OperationResult.Text = "删除用户信息成功！"; divResult.Visible = true;
                this.Query();
            }
            else
            {
                OperationResult.Text = "删除用户信息失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("删除用户信息失败", ex);
            OperationResult.Text = "删除用户信息失败！"; divResult.Visible = true;
        }
    }

    protected void GvDelete_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();
        string strRoleID = gvr.Cells[5].Text.Trim();
        try
        {
            int rtn = this.DeleteInfo(strKey, strRoleID);
            if (rtn == 1)
            {
                OperationResult.Text = "删除用户信息成功！"; divResult.Visible = true;
                this.Query();
            }
            else
            {
                OperationResult.Text = "删除用户信息失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("删除用户信息失败", ex);
            OperationResult.Text = "删除用户信息失败！"; divResult.Visible = true;
        }
    }

    protected void GvEdit_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();

        this.DisplayEditInfo(strKey);
        this.SetPanelVisiable(EditPanel);
    }

    protected void GvDetail_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();
        string strRoleID = gvr.Cells[5].Text.Trim();
        this.DisplayDetailInfo(strKey, strRoleID);
        this.SetPanelVisiable(DetailPanel);
    }

    protected void SetPanelVisiable(Panel panel)
    {
        EditPanel.Visible = false;
        DetailPanel.Visible = false;
        CreatePanel.Visible = false;
        SummaryPanel.Visible = false;

        panel.Visible = true;
    }

    protected int DeleteInfo(string strKey, string strRoleID)
    {
        //执行删除操作。
        return dalTuser.DeleteUser(strKey, strRoleID);
    }

    protected void DisplayDetailInfo(string strKey, string strRoleID)
    {
        try
        {
            //这里查询信息
            MoTuser mod = new MoTuser();
            mod.Uid = strKey;
            dalTuser.GetEntityEx(ref mod);

            //然后给查看视图赋值。
            DetailUID.Text = mod.Uid;

            MoEmployee modemp = new MoEmployee();
            modemp.EmpId = mod.EmpId;

            daEmp.GetEntityEx(ref modemp);

            DetailEmp_ID.Text = modemp == null ? "" : modemp.Fullname;
            MoDept moddept = new MoDept();
            moddept.DeptId = modemp.DeptId;

            dalDept.GetEntityEx(ref moddept);


            DetailDept_ID.Text = moddept == null ? "" : moddept.DeptName;
            DetailDisplay_Name.Text = mod.DisplayName;
            DetailPWD.Text = mod.Pwd; // "******";
            DetailStatus.Text = mod.Status == 2 ? "启用" : "禁用";
            DetailCreate_Time.Text = mod.CreateTime.ToString("yyyy-MM-dd");
            DetailUpdate_Time.Text = mod.UpdateTime.ToString("yyyy-MM-dd");
            DetailDisplay_SignaturePic.ImageUrl = "getfile.aspx?fileid=" + mod.Signaturepic;
            HDDetailRoleID.Value = strRoleID;
            UserContext uc = dalTuser.GetUserContext(strKey);
            if (uc != null)
            {
                DetailUserGroup.Text = uc.RoleName;
            }

            this.SetPanelVisiable(DetailPanel);
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "查询用户失败！"; divResult.Visible = true;
        }
    }

    protected void DisplayEditInfo(string strKey)
    {
        try
        {
            //这里查询信息
            MoTuser mod = new MoTuser();
            mod.Uid = strKey;
            dalTuser.GetEntityEx(ref mod);

            //然后给编辑视图赋值。
            MoEmployee modemp = new MoEmployee();
            modemp = new MoEmployee();
            modemp.EmpId = mod.EmpId;
            daEmp.GetEntityEx(ref modemp);

            EditDept_ID.SelectedValue = modemp == null ? "" : modemp.DeptId;
            this.EditDept_ID_SelectedIndexChanged(null, null);
            EditUID.Text = mod.Uid;
            EditEmp_ID.SelectedValue = modemp == null ? "" : mod.EmpId;
            EditDisplay_Name.Text = mod.DisplayName;
            //EditPWD.Text = mod.Pwd;
            //EditPWDConfirm.Text = mod.Pwd;
            EditPWD.Attributes.Add("Value", mod.Pwd);
            EditPWDConfirm.Attributes.Add("Value", mod.Pwd);
            EditStatus.SelectedValue = mod.Status.ToString();

            EditDept_ID.Enabled = false;
            EditEmp_ID.Enabled = false;

            UserContext uc = dalTuser.GetUserContext(strKey);
            if (uc != null)
            {
                if (!string.IsNullOrEmpty(uc.RoleID))
                {
                    EditUserGroup.SelectedValue = uc.RoleID;
                    HFEditRoleID.Value = uc.RoleID;
                }
            }
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "查询用户失败！"; divResult.Visible = true;
        }
    }

    protected void CreateCancel()
    {
        CreateDept_ID.SelectedIndex = 0;
        CreateEmp_ID.Items.Clear();
        CreateDisplay_Name.Text = "";
        CreatePWD.Text = "";
        CreatePWDConfirm.Text = "";
    }

    protected void BtnCreateUser_Click(object sender, EventArgs e)
    {
        this.CreateCancel();
        this.SetPanelVisiable(CreatePanel);
    }

    protected void CreateDept_ID_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateEmp_ID.Items.Clear();

        string deptid = CreateDept_ID.SelectedValue;
        if (!string.IsNullOrEmpty(deptid))
        {
            Utility.BindDataSet2Combox(daEmp.GetNoUserEmpByDeptID(deptid), CreateEmp_ID, "FullName", "Emp_Id");
            CreateDisplay_Name.Text = CreateEmp_ID.SelectedItem == null ? "" : CreateEmp_ID.SelectedItem.Text;
        }
    }

    protected void GvEnabled_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();
        int rtn;
        MoTuser mod = new MoTuser();
        mod.Uid = strKey;
        dalTuser.GetEntityEx(ref mod);

        if (btn.Text == "启用")
        {
            mod.Status = 2;
        }
        else
        {
            mod.Status = 1;
        }
        mod.UpdateTime = DateTime.Now;
        try
        {
            rtn = dalTuser.Update(mod);
            if (rtn == 1)
            {
                OperationResult.Text = btn.Text + "用户成功！";
                divResult.Visible = true;
                this.Query();
            }
            else
            {
                OperationResult.Text = btn.Text + "用户失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error(btn.Text + "用户失败", ex);
            OperationResult.Text = btn.Text + "用户失败！"; divResult.Visible = true;
        }
    }

    protected void EditDept_ID_SelectedIndexChanged(object sender, EventArgs e)
    {
        EditEmp_ID.Items.Clear();

        string deptid = EditDept_ID.SelectedValue;
        if (!string.IsNullOrEmpty(deptid))
        {
            Utility.BindDataSet2Combox(daEmp.GetEmploreeByDeptID(deptid), EditEmp_ID, "FullName", "Emp_Id");
        }
    }

    protected void CreateEmp_ID_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateDisplay_Name.Text = CreateEmp_ID.SelectedItem == null ? "" : CreateEmp_ID.SelectedItem.Text;
    }

    /// <summary>
    /// 隐藏刷新
    /// </summary>
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        this.Query();
    }

    protected void btnDeleteFile_Click(object sender, EventArgs e)
    {
        ImageButton btn = sender as ImageButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();

        try
        {
            if (dalTuser.RemoveSignaturePic(strKey))
            {
                this.Query();
            }
            else
            {
                OperationResult.Text = "删除用户签名图片失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "删除用户签名图片失败！"; divResult.Visible = true;
        }
    }
}
